﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Models;
using DAL;
using System.Data.SqlClient;

namespace 公司CRM客户关系系统.Customer
{
    /// <summary>
    /// QuanBuAll 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class QuanBuAll : System.Web.Services.WebService
    {
        /// <summary>
        /// 查询 VV_Customers 表的全部内容
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public PagingListForJson<VV_Customers> paging(int pageSize, int pageIndex)
        {
            //查询出分页数据
            PagingList<VV_Customers> list = BaseDAL.Paging<VV_Customers>(pageSize, pageIndex, "CusState=1", false);
            //为了不丢失总记录数这个字段，对类型进行转换
            PagingListForJson<VV_Customers> jsonList = new PagingListForJson<VV_Customers>();
            jsonList.total = list.RecordCount;
            jsonList.data = list;
            return jsonList;
        }
        [WebMethod]
        public PagingListForJson<VV_CustomLosts> SelectCustomLosts(int pageSize, int pageIndex)
        {
            //查询出分页数据
            PagingList<VV_CustomLosts> list = BaseDAL.Paging<VV_CustomLosts>(pageSize, pageIndex, false);
            //为了不丢失总记录数这个字段，对类型进行转换
            PagingListForJson<VV_CustomLosts> jsonList = new PagingListForJson<VV_CustomLosts>();
            jsonList.total = list.RecordCount;
            jsonList.data = list;
            return jsonList;

        }
        /// <summary>
        /// 添加流失报警
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public int AddLosts()
        {
            List<Activitys> list = BaseDAL.Select<Activitys>(@"insert into customlosts
            select cusid,max(actdate) lastdate,getdate(),'',null,1,1 from activitys
            where cusid not in (select cusid from customlosts where clstate in (1,3))
            group by cusid having datediff(dd, max(actdate), getdate())>=90", null);

            return list.Count;

        }
        /// <summary>
        /// 查询 VV_Customers 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public VV_Customers FindCustomers(int id)
        {
            return BaseDAL.Find<VV_Customers>(id);
        }
        /// <summary>
        /// 删除 Customers 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public VV_CustomLosts FindCustomLosts(int id)
        {
            return BaseDAL.Find<VV_CustomLosts>(id);
        }
        /// <summary>
        /// 查询 LinkMans 表的全部内容
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public List<LinkMans> SelectLinkMans(string CusID)
        {
            SqlParameter[] pars=new SqlParameter[1];
            pars[0] = new SqlParameter("@ID", CusID );
            return BaseDAL.Select<LinkMans>("select * from LinkMans where CusID = @ID", pars);
        }
        /// <summary>
        /// 查询 LinkMans 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public LinkMans FindLinkMans(int id)
        {
            return BaseDAL.Find<LinkMans>(id);
        }
        /// <summary>
        /// 删除 LinkMans 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLinkMans(int id)
        {
            return BaseDAL.Delete<LinkMans>(id);
        }
        
        /// <summary>
        /// 查询 Activitys 表的全部内容
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public List<Activitys> SelectActivitys(string CusID)
        {
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@ID",  CusID );
            return BaseDAL.Select<Activitys>("select * from Activitys where CusID = @ID", pars);
        }
        /// <summary>
        /// 查询 Activitys 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public Activitys FindActivitys(int id)
        {
            return BaseDAL.Find<Activitys>(id);
        }
        /// <summary>
        /// 删除 Activitys 表的单个内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteActivitys(int id)
        {
            return BaseDAL.Delete<Activitys>(id);
        }
        

    }
}
